package com.sc.nft.consume;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.SecondOrder;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.enums.LinkTypeEnums;
import com.sc.nft.enums.MessageTopic;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.enums.task.ActivityTaskTypeEnum;
import com.sc.nft.enums.task.TaskOrderTypeEnum;
import com.sc.nft.enums.task.TaskTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ActivityTaskService;
import com.sc.nft.service.TaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@RocketMQMessageListener(topic = MessageTopic.SECOND_ORDER_TOPIC + "${spring.profiles.active}", consumerGroup = MessageTopic.SECOND_ORDER_TOPIC + "${spring.profiles.active}")
@Component
@Slf4j
@RequiredArgsConstructor
public class SecondOrderConsumer implements RocketMQListener<SecondOrder> {

    private final TaskService taskService;
    private final UserInfoDao userInfoDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final ActivityTaskService activityTaskService;
    private final RedisTemplate redisTemplate;

    @Override
    public void onMessage(SecondOrder secondOrder) {
        digitalCollectionDao.newDealSave(secondOrder.getCollectionId(), secondOrder.getBuyPrice());
        String key = RedisKeyEnum.SECOND_ACTIVITY_TASK_LOCK.getKey() + secondOrder.getOrderNo();
        Long increment = redisTemplate.opsForValue().increment(key);
        redisTemplate.expire(key,180,TimeUnit.MINUTES);
        log.info("二级订单任务{},第{}次进入",secondOrder.getOrderNo(),increment);
        if(increment.longValue()>1L){
            return;
        }
        UserInfo userInfo = userInfoDao.getById(secondOrder.getBuyUserId());
        try {
            taskService.finishTask(userInfo, TaskOrderTypeEnum.SECOND, TaskTypeEnum.NEW_BUY, secondOrder.getId(), secondOrder.getCollectionId(), secondOrder.getBuyPrice());
            taskService.finishTask(userInfo, TaskOrderTypeEnum.SECOND, TaskTypeEnum.BUY_APPOINT, secondOrder.getId(), secondOrder.getCollectionId(), secondOrder.getBuyPrice());
            taskService.finishTask(userInfo, TaskOrderTypeEnum.SECOND, TaskTypeEnum.TOTAL_SPEND, secondOrder.getId(), secondOrder.getCollectionId(), secondOrder.getBuyPrice());
            UserInfo inviteUser = userInfoDao.getById(userInfo.getRecommendUserId());
            if (ObjectUtil.isNull(inviteUser)) {
                return;
            }
            taskService.finishTask(inviteUser, TaskOrderTypeEnum.SECOND, TaskTypeEnum.DAO, secondOrder.getId(), secondOrder.getCollectionId(), secondOrder.getBuyPrice());
            log.info("星球任务完成：{}",secondOrder.getOrderNo());
            // 用户最新成交价

            activityTaskService.finishTask(secondOrder.getCollectionId(), secondOrder.getBuyUserId(), 1, secondOrder.getBuyPrice(), ActivityTaskTypeEnum.BUY_APPOINT, LinkTypeEnums.SECOND, secondOrder.getId(), false);
            activityTaskService.finishTask(secondOrder.getCollectionId(), secondOrder.getBuyUserId(), 1, secondOrder.getBuyPrice(), ActivityTaskTypeEnum.TOTAL_BUY, LinkTypeEnums.SECOND, secondOrder.getId(), false);
            activityTaskService.finishTask(secondOrder.getCollectionId(), secondOrder.getBuyUserId(), 1, secondOrder.getBuyPrice(), ActivityTaskTypeEnum.TOTAL_SPEND, LinkTypeEnums.SECOND, secondOrder.getId(), false);
            log.info("活动任务完成：{}",secondOrder.getOrderNo());
        } catch (Exception e) {
            log.error("任务奖励发放失败", e);
        }

    }
}
